package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PowerDTO;
import com.ruoyi.system.domain.PowerGraphicDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IotCubePowerHistoryMapper;
import com.ruoyi.system.domain.IotCubePowerHistory;
import com.ruoyi.system.service.IIotCubePowerHistoryService;

/**
 * IoT数据_六面控温功率记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class IotCubePowerHistoryServiceImpl extends ServiceImpl<IotCubePowerHistoryMapper,IotCubePowerHistory> implements IIotCubePowerHistoryService
{
    @Autowired
    private IotCubePowerHistoryMapper iotCubePowerHistoryMapper;

    /**
     * 查询IoT数据_六面控温功率记录
     * 
     * @param id IoT数据_六面控温功率记录主键
     * @return IoT数据_六面控温功率记录
     */
    @Override
    public IotCubePowerHistory selectIotCubePowerHistoryById(Long id)
    {
        return this.getById(id);
    }

    /**
     * 查询IoT数据_六面控温功率记录列表
     * 
     * @param iotCubePowerHistory IoT数据_六面控温功率记录
     * @return IoT数据_六面控温功率记录
     */
    @Override
    public List<IotCubePowerHistory> selectIotCubePowerHistoryList(IotCubePowerHistory iotCubePowerHistory)
    {
        return iotCubePowerHistoryMapper.selectIotCubePowerHistoryList(iotCubePowerHistory);
    }

    /**
     * 新增IoT数据_六面控温功率记录
     * 
     * @param iotCubePowerHistory IoT数据_六面控温功率记录
     * @return 结果
     */
    @Override
    public boolean insertIotCubePowerHistory(IotCubePowerHistory iotCubePowerHistory)
    {
        return this.save(iotCubePowerHistory);
    }

    /**
     * 修改IoT数据_六面控温功率记录
     * 
     * @param iotCubePowerHistory IoT数据_六面控温功率记录
     * @return 结果
     */
    @Override
    public boolean updateIotCubePowerHistory(IotCubePowerHistory iotCubePowerHistory)
    {
        return this.updateById(iotCubePowerHistory);
    }

    /**
     * 批量删除IoT数据_六面控温功率记录
     * 
     * @param ids 需要删除的IoT数据_六面控温功率记录主键
     * @return 结果
     */
    @Override
    public boolean deleteIotCubePowerHistoryByIds(Long[] ids)
    {
        return super.removeByIds(Arrays.asList(ids));
    }

    /**
     * 删除IoT数据_六面控温功率记录信息
     * 
     * @param id IoT数据_六面控温功率记录主键
     * @return 结果
     */
    @Override
    public boolean deleteIotCubePowerHistoryById(Long id)
    {
        return this.removeById(id);
    }

    @Override
    public PowerDTO getRealTimePower(IotCubePowerHistory query) {
        PowerDTO powerDTO = new PowerDTO();

        List<IotCubePowerHistory> resultList = iotCubePowerHistoryMapper.getList(query);
        List<PowerGraphicDTO> list = new ArrayList<>();
        List<BigDecimal> vauleList = new ArrayList<>();
        handleData(resultList, list, vauleList, query.getType());
        powerDTO.setList(list);
        if (CollectionUtil.isNotEmpty(vauleList)) {
            powerDTO.setMax(Collections.max(vauleList));
            powerDTO.setMin(Collections.min(vauleList));
            BigDecimal sum = vauleList.stream()
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            int count = vauleList.size();
            BigDecimal divide = sum.divide(new BigDecimal(count), 2, RoundingMode.HALF_UP);
            powerDTO.setAgv(divide);
        }
        return powerDTO;
    }

    private static void handleData(List<IotCubePowerHistory> resultList, List<PowerGraphicDTO> list, List<BigDecimal> vauleList, Integer type) {
        resultList.forEach(item -> {
            PowerGraphicDTO powerGraphicDTO = new PowerGraphicDTO();
            powerGraphicDTO.setTime(item.getCreateTime().format(DateTimeFormatter.ofPattern("HH:mm")));
            switch (type) {
                case 1:
                    powerGraphicDTO.setData(item.getPowerTotal());
                    if (item.getPowerTotal() != null) {
                        vauleList.add(item.getPowerTotal());
                    }
                    break;
                case 2:
                    powerGraphicDTO.setData(item.getPowerAc1());
                    if (item.getPowerAc1() != null) {
                        vauleList.add(item.getPowerAc1());
                    }
                    break;
                case 3:
                    powerGraphicDTO.setData(item.getPowerAc2());
                    if (item.getPowerAc2() != null) {
                        vauleList.add(item.getPowerAc2());
                    }
                    break;
                case 4:
                    powerGraphicDTO.setData(item.getPowerLc1());
                    if (item.getPowerLc1() != null) {
                        vauleList.add(item.getPowerLc1());
                    }
                    break;
                case 5:
                    powerGraphicDTO.setData(item.getPowerLc2());
                    if (item.getPowerLc2() != null) {
                        vauleList.add(item.getPowerLc2());
                    }
                    break;
                default:
                    powerGraphicDTO.setData(item.getPowerTotal());
                    if (item.getPowerTotal() != null) {
                        vauleList.add(item.getPowerTotal());
                    }
                    break;
            }

            list.add(powerGraphicDTO);
        });
    }
}
